﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using Newtonsoft.Json.Linq;
using BLL;
using DAL;
using IDAL;
/***************************
 * 数据访问层
 * 现场照片数据
 * Author：JXL
 ***************************/ 
namespace EstateService.Controllers
{
    public class PhotoController : ApiController
    {
        Factory<IPhotoDAL, PhotoDAL> fa = new Factory<IPhotoDAL, PhotoDAL>();
        // GET api/photo
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/photo/5
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Photo> Get(int id)
        {
            string wherestr = "projectNumber=" + id + "";
            return fa.bll().getlist(wherestr);
        }

        // POST api/photo
        /// <summary>
        /// 进行数据添加
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Post([FromBody]dynamic value)
        {
            var name = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>(Convert.ToString(value.name));
            var url = value.url;
            List<Photo> list = new List<Photo>();
            for (var i = 0; i < name.Length; i++)
            {
                Photo la = new Photo()
                {
                    projectNumber = value.projectNumber,
                    photo_Name = name[i],
                    photo_Url = url[i]
                };
                list.Add(la);
            }
            return fa.bll().insert(list);
        }

        // PUT api/photo/5
        public int Put(int id, [FromBody]dynamic value)
        {
            var name = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>(Convert.ToString(value.name));
            var url = value.url;
            List<Photo> list = new List<Photo>();
            for (var i = 0; i < name.Length; i++)
            {
                Photo la = new Photo()
                {
                    projectNumber = value.projectNumber,
                    photo_Name = name[i],
                    photo_Url = url[i]
                };
                list.Add(la);
            }
            string wherestr = "projectNumber=" + id + "";
            
            return fa.bll().updates(list, wherestr);
        }

        // DELETE api/photo/5
        public void Delete(int id)
        {
        }
    }
}
